package com.pi.smart.wxmini.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pi.base.dto.result.AppResult;
import com.pi.base.util.time.DateTools;
import com.pi.dc.dao.entity.DcDeviceInfoEntity;
import com.pi.dc.service.DcBedService;
import com.pi.dc.service.DcDeviceInfoService;
import com.pi.dc.service.HcUserSleepLogService;
import com.pi.dc.vo.UserSleepInfoVo;
import com.pi.http.base.controller.BaseController;

@RequestMapping("/user/sleep")
@RestController
public class SmartUserSleepController extends BaseController{
  
  @Autowired
  private HcUserSleepLogService userSleepLogService;
  @Autowired
  private DcBedService bedNatureService;
  @Autowired
  private DcDeviceInfoService deviceInfoService;
  
  @RequestMapping("/info")
  public AppResult queryUserSleepInfo(Long shareId, String bedid){
    List<DcDeviceInfoEntity> devInfoList = deviceInfoService.validAndQueryDevInfo(getLoginUserId(), shareId, StringUtils.isNotBlank(bedid)?Long.parseLong(bedid):null);
    if(CollectionUtils.isEmpty(devInfoList)) {
      return AppResult.newFailResult("SMART_SLEEP.SLEEP_INFO_EMPTY", null);
    }
    Long dateBegin = DateTools.getDateBegin(new Date()).getTime();
    UserSleepInfoVo info = 
        userSleepLogService.queryUserSleepVo(devInfoList.get(0).getOwnerId(), devInfoList.get(0).getId(), new Date(dateBegin - 3600000 * 2), new Date(dateBegin + 3600000 * 22), 3600);
    return AppResult.newSuccessResult(info);
  }
  
  @RequestMapping("/quality")
  public AppResult queryUserSleepQuality(Long shareId, String bedid, String startDate, String endDate, Integer interval) throws ParseException{
    List<DcDeviceInfoEntity> devInfoList = deviceInfoService.validAndQueryDevInfo(getLoginUserId(), shareId, StringUtils.isNotBlank(bedid)?Long.parseLong(bedid):null);
    if(CollectionUtils.isEmpty(devInfoList)) {
      return AppResult.newFailResult("SMART_SLEEP.SLEEP_INFO_EMPTY", null);
    }
    Date start = DateTools.parserDate(startDate, DateTools.FORMAT_SIMPLEDATE);
    Date end = DateTools.parserDate(endDate, DateTools.FORMAT_SIMPLEDATE);
//    if(37994073973325824L == getLoginUserId().longValue()) {
//      UserSleepInfoVo info =new UserSleepInfoVo();
//      if(end.getTime() - start.getTime() > 604800000) {
//        String[] dateDesc = new String[] {"1号","2号","3号","4号","5号","6号","7号","8号","9号","10号","11号","12号","13号","14号","15号","16号","17号","18号","19号","20号","21号","22号","23号","24号","25号","26号","27号","28号","29号", "30号"};
//        int[] quality = new int[] {65,67,68,69,72,66,69,75,72,71,76,77,79,80,78,81,83,85,87, 86,88,95,84,85, 86, 88, 90, 93, 94, 95};
//        info.setDateDesc(dateDesc);
//        info.setSleepQuality(quality);
//        info.setOverAll("89.3分");
//      }else {
//        String[] dateDesc = new String[] {"24号","25号","26号","27号","28号","29号", "30号"};
//        int[] quality = new int[] {75, 78, 80, 84, 86, 90, 88};
//        info.setDateDesc(dateDesc);
//        info.setSleepQuality(quality);
//        info.setOverAll("83.3分");
//      }
//      return AppResult.newSuccessResult(info);
//    }
//    String[] dateDesc;
//    int[] quality;
//    if(startDate.equals(endDate)) {
//      dateDesc = new String[] {"1点","2点","3点","4点","5点","6点","7点","8点","9点","10点","11点","12点","13点","14点","15点","16点","17点","18点","19点","20点","21点","22点","23点","24点"};
//      quality = new int[] {90,90,60,50,80,70,65,5,0,0,0,0,0,0,0,0,0,0,0,0,15,65,90,90};
//    }else {
//      start = DateTools.getDateBegin(start);
//      end = DateTools.getDateBegin(end);
//      dateDesc = new String[] {"1号","2号","3号","4号","5号","6号","7号","8号","9号","10号","11号","12号","13号","14号","15号","16号","17号","18号","19号","20号","21号","22号","23号","24号"};
//      quality = new int[] {90,90,60,50,80,70,65,5,0,0,0,0,0,0,0,0,0,0,0,0,15,65,90,90};
//    }
//    UserSleepInfoVo info =new UserSleepInfoVo();
//    
//    info.setDateDesc(dateDesc);
//    info.setSleepQuality(quality);
//    info.setOverAll("89.3分");
    UserSleepInfoVo info = 
        userSleepLogService.queryUserSleepVo(devInfoList.get(0).getOwnerId(), devInfoList.get(0).getId(), start, end, interval);
    return AppResult.newSuccessResult(info);
  }
  
  @RequestMapping("/quality/rank/overnumber")
  public AppResult getUserSleepQualityOvernumber() {
    return AppResult.newSuccessResult(userSleepLogService.getUserSleepQualityOvernumber(getLoginUserId()));
  }
  
  @GetMapping("/tips")
  public AppResult getUserSleepTips() {
    return AppResult.newSuccessResult("不同年龄段的每天最佳睡眠时间是不同的，应按照自己的年龄科学睡眠。60岁以上：5.5~7h；30~60岁，7h左右；13~29岁，8h左右；4~12岁，10~12h；1~3岁，晚上12h，白天2~3h；1岁以下，16h。");
  }
}
